package com.example.demo01.controller;

import com.example.demo01.Entity.SalesFunnel;
import com.example.demo01.Entity.SalesOpportunity;
import com.example.demo01.Service.ClientService;
import com.example.demo01.Service.SalesOpportunityService;
import com.example.demo01.Service.SalesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/sales")
public class SalesController {

    @Autowired
    private SalesService salesService;

    @Autowired
    private ClientService clientService;
    @Autowired
    private SalesOpportunityService salesOpportunityService;


    // 更新销售阶段（销售漏斗管理）
    // 销售机会列表和录入页面
    @GetMapping("/manage")
    public String manageOpportunities(Model model) {
        // 获取所有销售机会
        List<SalesOpportunity> opportunities = salesOpportunityService.getAllOpportunities();
        model.addAttribute("opportunities", opportunities);  // 将销售机会数据传递到页面
        model.addAttribute("salesOpportunity", new SalesOpportunity());  // 用于新增销售机会的表单
        return "sales-funnel";  // 返回销售机会列表页面
    }

    // 处理销售机会录入的POST请求
    @PostMapping("/manage")
//    public String updateSalesStage(@RequestParam Long opportunityId,
//                                   @RequestParam String newStatus, Model model) {
//        try {
//            SalesOpportunity opportunity = salesOpportunityService.getOpportunityById(opportunityId);
//            if (opportunity != null) {
//                // 更新销售阶段
//                opportunity.setStage(newStatus);
//                salesOpportunityService.createOpportunity(opportunity);
//
//                model.addAttribute("message", "销售阶段更新成功!");
//            } else {
//                model.addAttribute("message", "未找到销售机会!");
//            }
//        } catch (Exception e) {
//            model.addAttribute("message", "发生错误: " + e.getMessage());
//        }
//        return "sales-funnel"; // 返回销售漏斗页面
//    }
    public String updateSalesStage(@RequestParam Long opportunityId,
                                   @RequestParam String newStatus, Model model) {
        try {
            // 调用 updateSalesStatus 方法
            salesService.updateSalesStatus(opportunityId, newStatus);

            // 更新成功的提示信息
            model.addAttribute("message", "销售阶段更新成功!");
        } catch (Exception e) {

            // 捕获异常并添加错误信息
            model.addAttribute("message", "发生错误: " + e.getMessage());
        }
        return "sales-funnel"; // 返回销售漏斗页面
    }


    // 查询销售阶段变更历史

    @GetMapping("/funnel/history")
    public String getSalesFunnelHistory(@RequestParam Long opportunityId, Model model) {
        try {
            List<SalesFunnel> history = salesService.getSalesFunnelHistory(opportunityId);
            model.addAttribute("history", history);
            System.out.println("History: " + history);  // 打印历史记录，确认是否有数据
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("message", "发生错误: " + e.getMessage());
        }
        return "sales-funnel"; // 返回正确的模板名
    }


}
